wordpress blog connects with php website - php

I have wordpress blog and php website.
I'd like to put the information about new articles on wordpress blog to the php website.
I did something. I have titles of articles.
But I need featured images and descriptions as well.
Here is code which I put on my php website.
<?php
// connection with wordpress
$db = mysql_connect ("localhost", "...", "....");
mysql_select_db ("...", $db);
$result_redaktor = mysql_query("SELECT * FROM wp_posts WHERE post_status = 'publish' ORDER by post_date DESC LIMIT 10", $db);
if (!$result_redaktor)
{
exit (mysql_error());
}
if (mysql_num_rows($result_redaktor) > 0)
{
$myrow_redaktor = mysql_fetch_array($result_redaktor);
do
{
printf ("<div class='main'>
<a href='http://redaktor.11klassniki.ru/%s'>%s</a></div>"
, $myrow_redaktor["guid"], $myrow_redaktor["post_title"]);
}
while ($myrow_redaktor = mysql_fetch_array($result_redaktor));
}
else
{
echo "<p>no data.</p>";
exit ();
}
include ("blocks/bd.php");
?>
I don't know how to get featured image and text (lead, which goes before tag more).
P.S My site and my blog are in the same hosting.
This code allows to see titles. It works. But I need to add pictures to titles.
How can I customize next code
$thumbnails = get_posts('numberposts=10');
foreach ($thumbnails as $thumbnail) {
if ( has_post_thumbnail($thumbnail->ID)) {
echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
echo '</a>';
}
}
echo get_the_post_thumbnail( $post_id, $size, $attr );

You should use wordpress RSS, read this article for more info:
http://www.wpbeginner.com/beginners-guide/what-is-rss-how-to-use-rss-in-wordpress/
Or, if your site and your blog are in the same hosting, you should include wp-load.php file in your php site and use all the function wordpress to show posts.
One example from http://davidwalsh.name/wordpress-recent-posts:
// Include the wp-load'er
include('wp-load.php');
// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 10
));
// Do something with them
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', $post['post_title'], '</li>';
}
echo '</ul>';
Enjoy your code!

Related

WordPress Infinite Loop on Single Post

I've taken some code from https://gist.github.com/banago/5603826 so that I am able to do previous/next posts for a specific post on my WordPress site which is a custom post type of 'product'.
<?php
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '← Previous Post');
} else {
$first = new WP_Query('post_type=products&posts_per_page=1&order=DESC'); $first->the_post();
echo '← Previous Post';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post →');
} else {
$last = new WP_Query('post_type=products&posts_per_page=1&order=ASC'); $last->the_post();
echo 'Next Post →';
wp_reset_query();
};
?>
However it doesn't actually infinitely loop, if it's on the first or last post, the link outputs the page it's already on. An example can be found here - http://s860623395.websitehome.co.uk/products/bespoke-build/
Any solutions to this? Thanks!
Was just working on this myself the other day for a portfolio.
Here is what worked for me (adjusted for your product loop)
It should show a single next product indefinitely (infinite loop)
if( get_adjacent_post(false, '', true) ) {
$previous_link_url = get_permalink( get_previous_post() );
echo '← Next Product';
} else {
$first = new WP_Query('post_type=product&posts_per_page=1&order=DESC');
$first->the_post();
echo 'Next Product →';
wp_reset_query();
};
Just a note, next post is actually the previous post, It's a confusing thing, but when you view the latest post, be it portfolio, product or whatever, the next one is actually the one before, otherwise the next post after the most recent published post would then be the oldest one. This seems to be how most have done it within an infinite loop.
Like you, I tried many variations, none of which really worked, but this one did. Currently I'm using it in the Genesis framework for a portfolio CPT with a background image.
Here is the full working code below taken from a child theme:
add_action( 'genesis_after_content_sidebar_wrap', 'go_prev_next_post_nav_cpt', 999 );
/**
* Enable next link in portfolio.
*
* #since 1.0.0
*/
function go_prev_next_post_nav_cpt() {
echo '<div class="adjacent-entry-pagination">';
echo '<div class="wrap">';
if( get_adjacent_post(false, '', true) ) {
$prevThumb = get_the_post_thumbnail_url( get_previous_post(), 'full' );
$previous_link_url = get_permalink( get_previous_post() );
$prevTitle = get_the_title( get_previous_post() );
echo '<a class="post-link bg-image bg-overlay" href="' . $previous_link_url . '" style="background-image:url(' . $prevThumb . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . $prevTitle . '</h3></div></a>';
} else {
$first = new WP_Query('post_type=portfolio&posts_per_page=1&order=DESC');
$first->the_post();
$bg_image = get_the_post_thumbnail_url();
echo '<a class="post-link bg-image bg-overlay" href="' . get_permalink() . '" style="background-image: url(' . $bg_image . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . get_the_title() . '</h3></div></a>';
wp_reset_query();
};
echo '</div>';
echo '</div>';
}
From memory, I used this as a starting point (lot a trial and error!). https://gist.github.com/banago/5603826

How to display only certain categories in Wordpress

I'm working in Wordpress and implemented some PHP to display all categories on a page (by a shortcode). By clicking on a category it links to a new page displaying all posts of that very category.
How do I display only certain categories, for instance by id and/or name of the cateogry?
One post has two categories (a : A & B or A & C). So one post a always has one category A, and one category B or C.
Here's my code:
function swerft_categories( ){
ob_start();
$categories = get_categories();
echo '<div class="swerft_cat">';
foreach($categories as $category) {
echo '<div class="swerft_cat_single col-lg-3 col-md-3 col-sm-6 col-xs-12">';
echo '<div class="swerft_cat_single_inner">';
$thim_group_custom_title_bg_img = get_term_meta( $category->term_id, 'thim_group_custom_title_bg_img', true );
if ($thim_group_custom_title_bg_img) {
$image_id = $thim_group_custom_title_bg_img['id'];
if ($image_id) {
$post_thumbnail_img = wp_get_attachment_image_src( $image_id, 'full' );
echo '<img src="' . $post_thumbnail_img[0] . '" alt="' . $category->name . '" />';
}
}
echo '<h5>'. $category->name .'</h5>';
echo '<p>'. $category->description . $category->count . '<span> Seminare </span>' . '</p>';
echo '</div>';
echo '</div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'swerft_categories', 'swerft_categories' );
I tried this in the first few lines for example with no success:
function swerft_categories($args){
ob_start();
$args = array('hide_empty'=> 1,
'name' => 'B');
$categories = get_categories($args);
1) I want only one certain relation to be displayed. Let's say: only the relation of a : A & B
2) I want the count to only show the amount of posts based on the relation above.
3) By clicking on a category based on this relation, I want only those posts to be displayed of course.
I was able to find a solution for the above mentioned problem together with a colleague. See the code below, I hope it may help someone maybe; and thanks for anyone considering this task though ;)
// Function to only show individual seminars after click on category on category-page
function swerft_filter_posts_open_individual_seminare( $query ) {
$offene_seminare = isset($_GET['offene_seminare']) ? boolval($_GET['offene_seminare']) : false;
$individual_seminars_category_id = 91;
if($query->is_category() && $query->is_main_query()) {
if ($offene_seminare) {
$query->set( 'category__not_in', $individual_seminars_category_id );
} else {
$query->set( 'category__in', $individual_seminars_category_id );
}
}
}
add_action( 'pre_get_posts', 'swerft_filter_posts_open_individual_seminare' );
// Function to only count individual seminars in overview
function swerft_count_individual_seminars_in_category($category_id) {
$individual_seminars_category_id = 91;
$query_args = array(
'post_type' => 'post',
'category__and' => array($individual_seminars_category_id, $category_id)
);
$query = new WP_Query( $query_args );
$count = $query->found_posts;
return $count;
}

Exclude Wordpress Category

I have a loop do display the different categories to go on the archive page.
How can y only display the categories one time ?
Currently, if two posts had the same category, there is two time the category
This is my code below
<div class="row ptb-20">
<?php
$args = array(
'category_name' => 'actualites',
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="category-filter">
<div class="single-filter">
<?php
$categories = get_the_category();
$separator = ", ";
$output = ' ';
if ($categories) {
foreach ($categories as $category) {
$output .= '<li>' . $category->cat_name . '</li>';
}
echo trim($output, $separator);
}
?>
</div>
</div>
<?php
} // End while
} // End if
else { echo '<p>Aucune actualité trouvée</p>'; } ?>
<?php wp_reset_postdata(); ?>
</div>
Method 1: Exclude a Category from WordPress Using Plugin
First thing you need to do is to install and activate the Ultimate Category Excluder plugin. For more details, you should follow our guide on how to install a WordPress plugin.
Upon activation, you’ll need to go to Settings » Category Excluder page. It will display all the categories that are available on your WordPress blog.
Method 2: Exclude a Category from WordPress Homepage Using Code
This method requires you to add code to your WordPress files. If you haven’t done this before, then see our guide on how to copy and paste code snippets in WordPress.
You will need to add following code to your theme’s functions.php file or a site-specific plugin.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Don’t forget to replace ID (-5) with your category ID. It will hide all blog posts from homepage belonging to the category that matches this ID.
Note: Make sure to add a minus (-) sign with the category ID.
Please refer: https://www.wpbeginner.com/wp-tutorials/how-to-exclude-a-category-from-your-wordpress-homepage/
If I understand your issue correctly, you can introduce a variable in which you would remember what categories has already been used, so you don't include them more than once.
<div class="row ptb-20">
<?php
$args = array(
'category_name' => 'actualites',
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="category-filter">
<div class="single-filter">
<?php
$categories = get_the_category();
$categories_displayed = [];
$separator = ", ";
$output = ' ';
if ($categories) {
foreach ($categories as $category) {
if (!in_array($category->cat_name, $categories_displayed)) {
$output .= '<li>' . $category->cat_name . '</li>';
$categories_displayed[] = $category->cat_name;
}
}
echo trim($output, $separator);
}
?>
</div>
</div>
<?php
} // End while
} // End if
else { echo '<p>Aucune actualité trouvée</p>'; } ?>
<?php wp_reset_postdata(); ?>
</div>

Wordpress shortcode shows as plain text in one view, works in other

I have this complex loop:
<?php
$args = array(
'cat' => 54,
'order' => 'ASC',
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<h2>' . get_the_title() .'</h2>'
. get_the_post_thumbnail() .
'<p>' . get_the_content("...plačiau") . '</p>';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<h1 class="thetitle">' . $category->name . '<span>Išskleisti <i class="fa fa-arrow-circle-down"></i></span></h1>';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
foreach ($q as $key=>$values) {
echo $key;
echo '<div class="straipsniai">';
foreach ($values as $value){
if (count($values) == 1) {
echo '<div class="vienas">' . $value . '</div>';
} else if (count($values) == 2) {
echo '<div class="du">' . $value . '</div>';
} else if (count($values) == 3) {
echo '<div class="trys">' . $value . '</div>';
} else {
echo '<div>' . $value . '</div>';
}
}
echo '</div>';
}
?>
Which worked for me, gave me this nice list/accordion:
http://bruzienesklinika.lt/web/gydytojai/
Now, each person in the category has some articles as posts and I want a list of their articles under their description. (basic Title + exerpt + read more link)
I've tried to do this by using "List category posts" plugin which allows me to use [catlist id=24] shortcode, but the problem is that browser prints it as plain text, source shows [catlist id=24](You can open the most bottom "GYDYTOJA REUMATOLOGĖ" tab to see that). The shortcode does work inside page, which is rendered by single.php, but it does not show when rendered in the loop I gave you in the beginning of the question.
SO, the question is, how do I make the shortcode work in the initial list, where all the categories are listed with posts inside the accordion.
Now it is not the problem of this certain plugin because no shortcodes work in that accordion list.
Or maybe you have an idea how to do this the other way?

Wordpress & JQuery: integrating WP function in a external php file & Redirecting urls to theme area

I have a external php file that queries the WP database for posts. I use the WP_Query class to customize my queries. It runs fine if I'm within the template files, but it won't when I add a new php file.
I would like to include the functions that WP uses to my file.
Ajax-nao.php
$categoryid = $_GET['cat'];
$postnum = $_GET['ponum'];
$pagenum = $_GET['panum'];
$args = array (
'cat' => $categoryid,
'posts_per_page' => $postnum,
'paged' => $pagenum
);
$q = new WP_Query($args);
while( $q->have_posts()) : $q->the_post();
$post_excerpt = get_the_excerpt();
$wrapper = '<h2 class="column-titles">' . get_the_title() . '</h2>' . '<p>' . $post_excerpt . '</p>';
echo $wrapper;
exit();
endwhile;
echo "No results found!";
When I run it I get this error
Fatal error: Class 'WP_Query' not found in I:\xampp\htdocs\dmp\wp-content\themes\dmp-v3\Ajax-nao.php on line 11
Also, is there a way to for $.get() to load the my php in my theme folder and not in the root directory?
Thanks.
From some research, try adding this to the top of your PHP file and change PATHHERE to the actual path:
// Include WordPress
define('WP_USE_THEMES', false);
require_once('PATHHERE/wp-blog-header.php');
For reference:
I want to run wp_query on a separate php file for an ajax call
wordpress loop with jquery .load()
There's a couple of ways to do this. One is to simply create a template file without any header or footer:
<?php
/**
* Template Name: AJAX NAO
*/
$categoryid = $_GET['cat'];
$postnum = $_GET['ponum'];
$pagenum = $_GET['panum'];
$args = array (
'cat' => $categoryid,
'posts_per_page' => $postnum,
'paged' => $pagenum
);
$q = new WP_Query($args);
while( $q->have_posts()) : $q->the_post();
$post_excerpt = get_the_excerpt();
$wrapper = '<h2 class="column-titles">' . get_the_title() . '</h2>' . '<p>' . $post_excerpt . '</p>';
echo $wrapper;
exit();
endwhile;
echo "No results found!";
Then you can use that template for a new page in your WP admin. In your jQuery, call the full URL of that page.
The harder but preferred way is to create a plugin. You'd need something like:
<?php
/*
Plugin Name: My AJAX NAO
Description: Returns NAO
Version: 1.0
Author: You
Author URI: http://yoursite.com
*/
//
function nao_callback() {
$categoryid = $_POST['cat'];
$postnum = $_POST['ponum'];
$pagenum = $_POST['panum'];
$args = array (
'cat' => $categoryid,
'posts_per_page' => $postnum,
'paged' => $pagenum
);
$q = new WP_Query($args);
while( $q->have_posts()) : $q->the_post();
$post_excerpt = get_the_excerpt();
$wrapper = '<h2 class="column-titles">' . get_the_title() . '</h2>' . '<p>' . $post_excerpt . '</p>';
echo $wrapper;
exit();
endwhile;
echo "No results found!";
}
add_action('wp_ajax_nao', 'nao_callback');
add_action('wp_ajax_nopriv_nao', 'nao_callback');
And then in your jquery:
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
method: 'POST',
dataType: 'html',
data: {
action: 'nao',
cat: '1',
ponum: '2',
panum: '3'
},
success: function(data) {
$('.js-element').html(data);
}
});
Assuming js-element is the class name of the container you want to add the result to.

Categories