CSS Styling within some Wordpress PHP? - php

I have a wordpress code that I want to use, but I am trying to give it some styling.
<?php global $post;
$current_post = $post; // remember the current post
for($i = 1; $i <= 1; $i++):
$post = get_previous_post(); // this uses $post->ID
setup_postdata($post);
// do your stuff here
the_excerpt();
endfor;
$post = $current_post; // restore ?>
Namely where it says //do your stuff here, I need to add the permalink and CSS styling to the_excerpt. Currently the_excerpt pulls up thumbnails on my blog.
This is the code I use for getting 2 random thumbnails, hopefully this will give you an idea of what I want for the above code.
<?php
$args = array( 'numberposts' => 2, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<div class="postgallery" id="post-<?php the_ID(); ?>">
<a class="title" href="<?php the_permalink() ?>" rel="bookmark"><?php the_excerpt(); ?>
<?php
$thetitle = $post->post_title; /* or you can use get_the_title() */
$getlength = strlen($thetitle);
$thelength = 35;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo "...";
?>
</a><?php if ($options['tags']) : ?><?php endif; ?>
</div>
<?php endforeach; ?>

Wrap the_excerpt() in a set of div tags with an id or class and that will give you the hooks you need to style the content of the excerpt via the stylesheet.

Related

Pagination not working on Post name permalink but working on plain permalink

I am trying to loop through a post type called blog. The pagination works fine when the Wordpress permalinks are set to plain however when I change it to post the name and click to go on pagination link, it loads a 404 error.
I found out that you can't have the same post type and page name since it will cause a 404 error. I wanted to know if there was a workaround because changing the name of the post type will affect the blog posts.
My page-blog.php
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query( array( 'post_type' => 'blog',
'posts_per_page' => 2,
'paged' => $paged,
'has_archive' => false,
'rewrite' => array(
'slug' => '/blog', // if you need slug
'with_front' => false,
),)
);
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
// Set variables
$title = get_the_title();
$post_date = get_the_date('M j');
$amount_of_time_to_read = get_field('amount_of_time_to_read');
?>
<a href="<?php the_permalink(); ?>" class="post-blog-link">
<div class="post">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class="post-image-v2" style="background-image:url('<?php echo $url ?>');">
</div>
<div class="post-content-v2">
<h2 class="post-title"><?php echo $title; ?></h2>
<div class="post-excerpt">
<p><?php echo get_excerpt(); ?></p>
</div>
<p class="post-date"> <span class="caps"><?php echo $post_date; ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
</div>
</div>
</a>
<!--
-->
<?php endwhile; ?>
<center>
<div class="pagination mt-25">
<?php pagination_bar( $loop ); ?>
</div>
</center>
<?php wp_reset_postdata();
endif;
?>
My functions.php
add_action('init', 'custom_rewrite_basic');
function custom_rewrite_basic() {
global $wp_post_types;
foreach ($wp_post_types as $wp_post_type) {
if ($wp_post_type->_builtin) continue;
if (!$wp_post_type->has_archive && isset($wp_post_type->rewrite) && isset($wp_post_type->rewrite['with_front']) && !$wp_post_type->rewrite['with_front']) {
$slug = (isset($wp_post_type->rewrite['slug']) ? $wp_post_type->rewrite['slug'] : $wp_post_type->name);
$page = get_page_by_slug($slug);
if ($page) add_rewrite_rule('^' .$slug .'/page/([0-9]+)/?', 'index.php?page_id=' .$page->ID .'&paged=$matches[1]', 'top');
}
}
}
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
return ($page ? get_post($page, $output) : NULL);
}
what do you want to achieve to have a Post Type and Page to be of the same slug?
As per my understanding you want to display the archive of your custom post type "Blog". All you have to do is create a file name archive-blog.php and use the plain WordPress loop. That way you don't need to have a page-blog.php (Delete it) to display the Archives of your "Blog" post type. yourwebsite.com/blog will automatically display your "Blog" archive.
Use the code below to paste in your archive-blog.php
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();
// set vars
$amount_of_time_to_read = get_field('amount_of_time_to_read');
?>
<a href="<?php the_permalink(); ?>" class="post-blog-link">
<div class="post">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ), 'thumbnail' ); ?>
<div class="post-image-v2" style="background-image:url( '<?php echo $url ?>' );">
</div>
<div class="post-content-v2">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-excerpt">
<p><?php the_excerpt(); ?></p>
</div>
<p class="post-date"> <span class="caps"><?php the_date( 'M j' ); ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
</div>
</div>
</a>
<?php endwhile; ?>
<?php
// You need to tweak this function, it shouldn't be needing a $loop var to work
// paste the function here and may be we will take a look at that
// pagination_bar( $loop );
the_posts_pagination();
?>
<?php else : ?>
<?php // No Posts Found ?>
<?php endif; ?>
Had to add this to my functions.php
add_rewrite_rule('^blog/page/([0-9]+)','index.php?pagename=blog&paged=$matches[1]', 'top');

Query Page with Posts In WordPress

I am pulling posts from a specific category. This feature works. However, I also need it to pull pages every now and then and I can't figure out a way to get it to pull this in addition to the posts.
HTML/PHP
<?php query_posts('cat=8&posts_per_page=3'); while (have_posts()) : the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like to keep this structure and find a way to include pages, any feedback is helpful!
You cannot fetch posts and use this query as it is. WordPress posts have no categories by default so the cat=x part will always exclude automatically all posts.
I think there is no better solution than using a second query. Depending on what you are using this for, it may be better to separate these loops.
If you want to use a single loop for multiple queries consider merging the query like mentioned here:
<?php
$query1 = new WP_Query(array(
'cat' => 8,
'post_type' => 'post',
'posts_per_page' => 3
));
$query2 = new WP_Query(array(
'post_type' => 'page',
));
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );
$wp_query->post_count = $query1->post_count + $query2->post_count;
?>
<?php while( $wp_query->have_posts() ): $wp_query->the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>

Advance Custom fields keep returning the same value

So to elaborate, I have 11 posts and in each of those posts I input an image into advance custom fields form. But when I call them I get back 11 results but from just 1 post.
Here is what I'm working with. Just to tell you this is in functiuons.php since I want to get this as a shortcode so I can use it on multiple post types.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('slider') ): ?>
<?php while( have_rows('slider') ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I have almost identical code on the template I created, but that one works, this one does not and I don't know why.
Pass id of current post for the acf have_rows() function. I have done that using , $currentId = get_the_ID(); and then using the variable $currentId where I need.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post );
$currentId = get_the_ID();
?>
<?php if( have_rows('slider', $currentId) ): ?>
<?php while( have_rows('slider', $currentId) ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I had to add
global $post;
to the top of my function for it to work. Thanks for your help anyway guys.

How to display on a single.php all posts of category, in which this single.php is situated

For example I have some category that includes five posts. When I select the post, need to display the rest four posts from category in which it situated.
<?php
$infocat = get_the_category();
$info = $infocat[0]->cat_ID;
$array = "orderby=rand&showposts=10&cat=$info";
query_posts($array);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a class="permlinccat" href="<?php the_permalink() ?>" title="Перейти к посту: <?php the_title(); ?>" ><?php the_title(); ?></a>
<?php endwhile; else: ?>
<?php endif; wp_reset_query(); ?>
Tried this function - almost good, but it get only titles of the posts
found a solution...
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category- >term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>5 //
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Current category</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ? >"><?php the_title(); ?>
<img src="<?php echo first_image() ?>"title="<?php the_title(); ?>" alt="<?php the_title(); ?>"/>
</a>
<?php
}
echo '</ul>';
}
}
?>
Try this :) Just simply get the current category and exclude the current post from the query .
<?php
$cat = get_the_category();
$catOK = $cat[0]->cat_ID; //if multiple categories
$postID = $post->ID;
query_posts(
array(
'cat' => $catOK,
'post__not_in' => array($postID)
)
);
while (have_posts()) : the_post(); ?>
YOUR HTML CODE
<?php endwhile; ?>

How to Achieve Something Like This In WordPress

Hey there, I'm trying to create a page showing specific pages (hope that makes sense), probably by calling their post ID's or something.
I want to pull in the page thumbnail/featured image, the page title, the page's description, and then a link to that page.
Something along the lines of this.
<ul>
<li>
<?php the_post_thumbnail(); ?>
<h2>Page Title</h2>
<p>Page Description</p>
Link to page
</li>
</ul>
Any help will be appreciated, thanks in advance.
UPDATE: At the moment I've got something like this. Using a custom field to bring in the description. I'm still trying to work out how I'd only show pages that are under a parent page called "Culture".
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>
UPDATE 2: Solved it! Used the following if anyone's interested.
Pulled in all subpages from parent page (id=7).
Then the post thumbnail, followed by the page title, description using a custom field called description and finally the permalink.
Hope this helps anyone in a similar situation.
<?php query_posts('post_type=page&post_parent=7'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('culture-page-listing'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>
I wrote a loop in WP some time ago which I'm sure isn't perfect, but it did basically something like that (delimited by categories).
http://www.kyleboddy.com/2010/10/14/wordpress-code-attachment-category-loop/
<?php
$areas = array(1 => 'Seattle','East Side & Mercer Island','North Side','South Side');
$slugs = array(1 => 'seattle-jobs','east-side-and-mercer-island-jobs','north-end-jobs','south-end-and-west-seattle-jobs');
$i = count($areas);
$n = 1;
while ($n <= $i)
{
global $post;
$myposts = get_posts('numberposts=-1&offset=0&category_name=' . $slugs[$n]);
echo '<div id="imageList">';
echo '<a name="' . $areas[$n] . '"></a><h2>' . $areas[$n] . '</h2>';
echo '<table id="ourwork"><tr>';
$x = 1;
foreach($myposts as $post)
{
setup_postdata($post);
echo '<td>';
$args = array(
'post_type' => 'attachment',
'numberposts' => '-1',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
$y = count($attachments);
$y--;
echo '<a href="' . $post->guid . '">';
echo wp_get_attachment_image($id = $attachments[$y]->ID, $size=array(200,133), $icon = false);
echo '<strong><br><br>';
echo apply_filters('the_title', $attachments[$y]->post_title);
echo '</strong></a>';
echo '</td>';
if ($x == 4)
{
echo '</tr><tr>';
$x = 0;
}
$x++;
}
}
echo '</tr></table>';
echo '</div><div class="blog"></div>';
$n++;
}

Categories