PHP不是我的专业领域,而且我一直在撞墙直到2-3天,现在试图解决这个问题。
我需要运行自定义WordPress查询,在页面上显示一些帖子,为了做到这一点,我安装了PHP Everywhere插件将PHP转换为短代码。
我有这个循环(它可能也可以使用一些工作)并且它本身工作正常,但插件的工作方式我需要运行具有不同类别ID的此循环的多个实例,以显示来自不同类别的帖子在同一页上。
这是我现在拥有的循环:
<?php
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post_square">
<a href="<?php the_permalink() ?>" class="post_image" style="background-image: url(<?php the_post_thumbnail_url(); ?>);"></a>
<div class="post_text">
<div class="post_title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
<div class="post_meta">By <?php $author = the_author_meta("display_name"); ?> / <?php echo get_the_date("F j, Y"); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( "Sorry, no posts matched your criteria" ); ?></p>
<?php endif; ?>
该插件要求我按如下方式格式化PHP,以在同一页面上运行多个实例:
<?php
if($instance=="1"){
echo("Instance 1");
}
if($instance=="2"){
echo("Instance 2");
}
?>
我的问题是重写我原来的PHP代码以适应第二个循环的回声。
我已经厌倦了下面这段代码了,但是它无效:
<?php
if ( $instance=="1" ) {
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="post_square">
<a href="'. the_permalink() .'" class="post_image" style="background-image: url('. the_post_thumbnail_url(); .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. the_permalink() .'">'. the_title(); .'</a></div>
<div class="post_meta">By '. $author = the_author_meta("display_name"); .' / '. echo get_the_date("F j, Y"); .'</div>
</div>
</div>';
endwhile;
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
wp_reset_postdata();
else:
_e( "Sorry, no posts matched your criteria" );
endif;
}
if ( $instance=="2" ) {
echo 'this is two';
}
?>
我在Dreamweaver中得到3个语法错误警告,但我不太清楚PHP知道哪个'。'它指的是:
我很茫然。我已经尝试了很多方法来做到这一点,但它超出了我的范围......
答案 0 :(得分:2)
看起来你必须在第10,12,13行;
后删除()
个字符。固定代码为:
<?php
if ( $instance=="1" ) {
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="post_square">
<a href="'. the_permalink() .'" class="post_image" style="background-image: url('. the_post_thumbnail_url() .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. the_permalink() .'">'. the_title() .'</a></div>
<div class="post_meta">By '. $author = the_author_meta("display_name") .' / '. echo get_the_date("F j, Y") .'</div>
</div>
</div>';
endwhile;
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
wp_reset_postdata();
else:
_e( "Sorry, no posts matched your criteria" );
endif;
}
if ( $instance=="2" ) {
echo 'this is two';
}
?>
不要在回声中使用WordPress的回声功能。您可以详细了解the_permalink
和get_the_permalink
here之间的差异。所以更好,工作代码是:
<?php
if ( $instance=="1" ) {
if ( get_query_var( "paged" ) ) { $paged = get_query_var( "paged" ); }
elseif ( get_query_var( "page" ) ) { $paged = get_query_var( "page" ); }
else { $paged = 1; }
$the_query = new WP_Query("cat=20&posts_per_page=5&paged=" . $paged);
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="post_square">
<a href="'. get_the_permalink() .'" class="post_image" style="background-image: url('. get_the_post_thumbnail_url() .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. get_the_permalink() .'">'. the_title() .'</a></div>
<div class="post_meta">By '. $author = get_the_author_meta("display_name") .' / '. get_the_date("F j, Y") .'</div>
</div>
</div>';
endwhile;
next_posts_link( "Older Entries", $the_query->max_num_pages = 3 );
previous_posts_link( "Newer Entries" );
wp_reset_postdata();
else:
_e( "Sorry, no posts matched your criteria" );
endif;
}
if ( $instance=="2" ) {
echo 'this is two';
}
?>
答案 1 :(得分:1)
这是你想要的:
echo '<div class="post_square">
<a href="'. the_permalink() .'" class="post_image" style="background-image: url('. the_post_thumbnail_url() .');"></a>
<div class="post_text">
<div class="post_title"><a href="'. the_permalink() .'">'. the_title() .'</a></div>
<div class="post_meta">By '. $author = the_author_meta("display_name") .' / '. get_the_date("F j, Y") .'</div>
</div>
</div>';