how to customize post in wordpress? - php

I try to customize my own post using the shortcode way in wordpress. I want to display the post according to its category.
<?php
function sample_post($atts) {
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<label><?php the_title(); ?></label>
<?php
endwhile;
endif;
}
add_shortcode('sample_post', 'sample_post');
?>
the code is fine and I have a file in my template name content-page.php
here is the code of my content-page
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'unite' ),
'after' => '</div>',
) );
edit_post_link( __( 'Edit', 'foodgrower' ), '<span class="edit-link">', '</span>' );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
I don't know if I miss something about the setup or query on it. Here's I really want to displays. I only want to display <label><?php the_title(); ?></label> (the title of the post), but now it displays the content-page.php. I don't get it also why it appears in my page. I didn't call the content-page.php to display in my page.

Change your code to return the data, like the following,
function sample_post($atts) {
$return = '';
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$return .= "<label>".the_title()."</label>";
endwhile;
endif;
return $return;
}
add_shortcode('sample_post', 'sample_post');

Related

Sidebar is appearing on custom post pages without being called

I'm creating a Wordpress theme for a graphic portfolio website, and I'm utilizing the sidebar.php for a hero image on the index page that is not meant to appear on other pages. While this works for all static inside pages, I have two custom post categories, and the sidebar section has started appearing above those posts.
I should note that this hasn't always been a problem, and started cropping up for no apparent reason while working on something else.
On index.php, the sidebar is called as follows:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; ?>
<nav>
<ul class="pager">
<li><?php next_posts_link( 'Previous' ); ?></li>
<li><?php previous_posts_link( 'Next' ); ?></li>
</ul>
</nav>
<?php endif;?>
<?php get_footer(); ?>
While page.php is similar but lacks the php line calling the sidebar, as follows:
<?php get_header(); ?>
<div class="row">
<div class="col-sm-12">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
page-fine-art.php also lacks that php line, as follows:
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'fine-art',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$custom_query = new WP_Query( $args );
while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="blog-post">
<h2 class="blog-post-title"><? php the_title(); ?></h2>
<p class="blog-post-meta"><?php the_date(); ?> by <?php the_author(); ?></p>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
The function creating the the custom posts for "fine art" goes:
function create_my_custom_posts() {
register_post_type( 'fine-art',
array(
'labels' => array(
'name' => __( 'Fine Art' ),
'singular_name' => __( 'Fine Art' ),
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'thumbnail',
'custom-fields'
)
));
I don't see where sidebar.php is being called in the final example, or where else it could be coming from.

Wordpress page content and posts in the same page

I'm pretty new in world of wordpress and I'm trying to adjust HTML page into wordpress theme.
I need a page content to be displayed first on a page and under that, the posts should be shown. But what I'm getting are just posts shown twice on the page (where page content should be). Is there any possibility to overcome this?
And additional question, how to filter posts according to their category? I've tried with query_posts('cat=Small'), but it doesn't seem to work properly.
The code for index.php looks as following:
<?php get_header(); ?>
<?php
wp_reset_query();
while ( have_posts() ) : the_post();
the_content();
endwhile;
wp_reset_query();
?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php query_posts('cat=Small'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Try the below code. This may help you
<section>
<div class="major">
<h2>Erat lacinia</h2>
</div>
<div class="features">
<?php $args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);?>
<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
You can use two loops.
In your php page template, first execute the regular loop to get the content of the actual page, like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//output page content here
<?php endif; ?>
Then you define a new query for the desired posts:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'ASC',
)
);
//(Add and change arguments as desired in the array above)
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
//Output the post contents in a loop here
<?php endif;
wp_reset_postdata();?>
And then add the rest of the page template (footer etc.)
<?php
/*
*Template name: test
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$attrs = array(
'numberposts' => 10,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'small' )
)
)
);
$my_posts = get_posts( $attrs );
the_content();
?>
<?php if ($my_posts): ?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php foreach ($my_posts as $key => $value): ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?= $value->post_title; ?></h3>
<p><?= $value->post_content ?></p>
</div>
</article>
<?php endforeach ?>
</div>
</section>
<?php endif ?>
<?php
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
get_footer(); ?>

Getting excerpt in loop within loop – Twenty sixteen theme Wordpress

Hello fellow wordpress expirienced users.
I’m having a struggle while doing a latest 3 posts excerpt with thumbnail in my homepage in Wordpress TwentySixteen Theme.
I tried various possibilities but i cant get it running.
The goal is to look the posts like this:
http://caenthemes.cekuj.net/?s=p%C5%99%C3%ADsp%C4%9Bvek
My thought is to use an already made template for the seach page.
But than it the text of the excerpt is nowhere:
http://caenthemes.cekuj.net/
The fact that its not styled leave aside please.
The code of the main page:
<?php
/**
* The template for displaying main-page without title.
* #package WordPress
* #subpackage Twenty_Sixteen
* #since Twenty Sixteen 1.0
*/
?>
<section id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!--<header class="entry-header">
<?php the_title('<h1 class="entry-title">', '</h1>'); ?>
</header><!-- .entry-header -->
<div class="wp-page-content">
<?php
the_content();
wp_link_pages(array(
'before' => '<div class="page-links"><span class="page-links-title">' . __('Pages:', 'twentysixteen') . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __('Page', 'twentysixteen') . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
));
?>
</div><!-- .entry-content -->
</section>
<section>
<header class="entry-header">
<h2>
<?php
if (get_locale() == 'cs_CZ') {
echo "Nejnovější příspěvky";
} else {
echo "Latest posts";
}
?>
</h2>
</header><!-- .entry-header -->
<?php
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post);
get_template_part('template-parts/content', 'search');
endforeach;
wp_reset_postdata();
?>
</section><!-- #wp-page-content-## -->
I’m 80% sure that i’m not handling right the inner loop within the main loop of twenty sixteen. Just to cover all posibilities i was also trying to do it not via get template but still i only get the posts categories titles and thumbnails but not the excerpt.
Can you help me with this ?
The whole page is based on twenty sixteen theme with my modifications.
Thank you very much,
Caen Ragestorm
For getting post details such as title,content and featured image you can use this code :
$latestPost = new WP_Query( array( 'post_type' => 'posts', 'posts_per_page' =>-1,'order' => 'ASC') );
while ( $latestPost->have_posts() ) : $latestPost->the_post();
$sTitle = the_title();
$sContent = the_content();
$feat_image_latestPost = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
endwhile;
Re write CSS according to your requirement.
Thanks to SJP i made it work. So the full code working for me is here, hope anyone else will find it usefull:
<section id="latest-posts">
<header class="entry-header">
<h2>
<?php
if (get_locale() == 'cs_CZ') {
echo "Nejnovější příspěvky";
} else {
echo "Latest posts";
}
?>
</h2>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
$args = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$latestPost = new WP_Query($args);
while ($latestPost->have_posts()) : $latestPost->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title(sprintf('<h2 class="entry-title">', esc_url(get_permalink())), '</h2>'); ?>
</header><!-- .entry-header -->
<div class="post-summary">
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
<?php the_post_thumbnail('post-thumbnail', array('alt' => the_title_attribute('echo=0'))); ?>
</a>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
</div>
<?php if ('post' === get_post_type()) : ?>
<footer class="entry-footer">
<?php twentysixteen_entry_meta(); ?>
<?php
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
__('Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen'), get_the_title()
), '<span class="edit-link">', '</span>'
);
?>
</footer><!-- .entry-footer -->
<?php else : ?>
<?php
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
__('Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen'), get_the_title()
), '<footer class="entry-footer"><span class="edit-link">', '</span></footer><!-- .entry-footer -->'
);
?>
<?php endif; ?>
</article>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</section>
Nice day to you all.
Caen Ragestorm
www.CaenRagestorm.cz

Wordpress static page template pagination not working

I'm building a custom page template and I can't seem to get the pagination to work. It keeps showing the same posts as on the home/first page. I have tried a lot of different code but getting the same problem with each results.
This is the current query I am using:
<?php
global $paged;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=2&post_type=post'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="news-item">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
?>
<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
Found the solution, this code worked:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => '2',
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="news-item">
<?php
if ( has_post_thumbnail() ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
echo '<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>';
}
else {
}
;?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
<div id="single-post-container"></div>
<a class="button-1 load-more" href="<?php echo get_permalink(); ?>">Lees meer</a>
<a class="ajax-close button-1" href="#">X</a>
</div>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Try this code just copy pase
<?php
$args = array(
'post_type'=> 'post',
//'category_name' => 'blog',
'orderby' => 'post_date',
//'posts_per_page'=>'1'
'paged' => get_query_var('paged')
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php // Wordpress Pagination
$big = 999999999; // need an unlikely integer
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '<',
'next_text' => '>',
'type' => 'array'
) );
if(!empty($links)){ ?>
<ul class="pagination">
<?php
foreach($links as $link){
?>
<li><?php echo $link; ?></li>
<?php
}
wp_reset_query(); ?>
</ul>
<?php } ?>

Only first post has content in loop

My Wordpress site has a problem:
In my blog-loop only the first post has content displayed; for the rest the_content(); never seemed to be called (checked with "inspect element").
Any probs in my code?
php/html:
<section id="blogPosts" class="clearfix">
<?php $myposts = get_posts('');
foreach($myposts as $post) :
setup_postdata($post);
?>
<div id="post-<?php the_ID(); ?>" class="post clearfix">
<div class="postHeader clearfix">
<a href="<?php the_permalink();?>">
<h2><?php the_title(); ?></h2>
</a>
<h4><?php the_date(); ?></h4>
</div>
<div class="postTags clearfix">
<ul class="tagContainer clearfix">
<?php the_tags( '<li><div class="tagInline">',
'</div></li><li><div class="tagInline">',
'</div></li>'); ?>
</ul>
</div>
<div class="blogContent">
<?php the_content();?>
</div>
<div class="editPost">
<?php edit_post_link('<h4>Redigera detta inlägg', '', '</h4>'); ?>
</div>
<?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>
<?php //comments_template(); ?>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</section>
Check all other posts for having content in it..
Posts can be get by
<?php $posts_array = get_posts( $args ); ?>
Default Usage of args:
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true ); ?>
Access all post data
<?php
$args = array( 'posts_per_page' => -1 );
$allposts = get_posts( $args );
foreach ( $allposts as $post ) :
setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endforeach;
wp_reset_postdata(); ?>
This is the simple code to get all the posts. You can check the code by print the data ( print_r($post); ) to check what you are getting in the loop.
If you're talking about the page that has been assigned to Posts under Wordpress Reading Settings, you can get rid of most of your code and simplify it:
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Just put whatever information you want between the while statement and you'll be good. See the Wordpress codex if you want to get more specific.

Categories